package com.springcloud.system.user.interceptor;

import com.springcloud.common.entity.SystemUser;
import com.springcloud.common.web.interceptor.AbstractUserInterceptor;
import com.springcloud.system.user.service.SystemUserService;
import com.springcloud.system.user.util.SystemUserContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.util.Optional;

/**
 * <p>  </p>
 *
 * @author mori
 * @since 11/8/2019 9:27 AM
 */
@Component
public class SystemUserInterceptor extends AbstractUserInterceptor<SystemUser> {

    private SystemUserService systemUserService;

    public SystemUserInterceptor(SystemUserContextHolder systemUserContextHolder, SystemUserService systemUserService) {
        super(systemUserContextHolder);
        this.systemUserService = systemUserService;
    }

    @Override
    public SystemUser getContext(String username) {
        return Optional.ofNullable(username)
                .filter(StringUtils::isNotBlank)
                .map(systemUserService::getSystemUserByUsername)
                .orElse(null);
    }


}
